use anyhow::Result;

// 20/PT1H
#[derive(Clone, Copy)]
pub struct RateLimit {
    pub burst: std::num::NonZeroU32,
    pub duration: std::time::Duration,
}

pub fn parse_rate_limit(s: &str) -> Result<RateLimit> {
    let tokens: Vec<&str> = s.splitn(2, "/").collect();
    let burst = tokens[0].parse::<std::num::NonZeroU32>()?;
    let duration = iso8601_duration::Duration::parse(tokens[1]).map_err(|e| anyhow::anyhow!("{:?}", e))?.to_std();
    Ok(RateLimit { burst, duration })
}
